package someTestExcemple.udp;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        //模拟客户端(比如qq)发送数据包给服务器
        // 创建客户端发送套接字
        DatagramSocket socket = new DatagramSocket();
        System.out.println("客户端启动-->InetAddress.getLocalHost()："+ InetAddress.getLocalHost());
        //创建数据包
        int count = 0;
        Scanner sc = new Scanner(System.in);
        while (count < 50) {
            System.out.println("请输入要发送的数据：");
            String msg = sc.nextLine();
            byte[] data = msg.getBytes();
            if(msg.equals("exit")) {
                System.out.println("数据发送结束~~~~");
                socket.close();
                break;
            }
            DatagramPacket packet = new DatagramPacket(data, data.length,
                    InetAddress.getLocalHost(), 10086);
            //发送数据包
            socket.send(packet);
            System.out.println("客户端数据发送完毕~~~"+count);
            //socket.close();
            count++;
        }
        //发送50条消息还没exit,就关闭socket
        socket.close();

    }
}
